home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Workspace / Briefcase / Source / Localization.m < prev    next >
Text File  |  1992-07-24  |  6KB  |  195 lines

  1. /*
  2.  * You may freely copy, distribute, and reuse the code in this example.
  3.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  4.  * fitness for any particular use.
  5.  * 
  6.  */
  7.  
  8. /* Last Edit:    Sep 1991.    for CanScan by UEKUSA.                    */
  9.  
  10. /* Modified hideously by Subrata K. Sircar for use with MultApp.    */
  11. /* My assumptions are that the application delegate will keep track    */
  12. /* of the string table, the app will load the appropriate nib in     */
  13. /* in the XXX_main.m file and images are stored in the appropriate     */
  14. /* directories.                                                        */
  15.  
  16. /* Known bugs:  the initialization each time of launchDir, etc. is    */
  17. /* wasteful, and if the user has no preference, systemLanguages        */
  18. /* will return NULL each time.  This should be an object with an    */
  19. /* init method, but that makes code much uglier...                    */
  20.  
  21. #import "Global.h"
  22. #import <appkit/NXImage.h>
  23. #import <appkit/NXBitmapImageRep.h>
  24.  
  25.  
  26. #define NATIVE_LANGUAGE        "English"
  27.         
  28. static char                    launchDir[MAXPATHLEN+1];
  29. static id                    stringSet        = nil;        
  30. static char *const            *languages;
  31.  
  32.  
  33. /* If the user has not set a language preference, load the nib
  34.  * section stored in the Mach-O by default. Otherwise,
  35.  * the nib file loaded would follow the language set in the Preferences
  36.  * Application.
  37.  */
  38.  
  39. id LoadLocalNib(const char *nibFile, id owner, BOOL withNames, NXZone *zone)
  40. {
  41.     BOOL found = NO;
  42.     id retval = nil;
  43.     char path[MAXPATHLEN+1];
  44.  
  45.     if (!launchDir || !*launchDir) {
  46.         if (retval = [NXApp delegate]) {
  47.             sprintf(launchDir,[retval launchDirectory]);
  48.             retval = NULL;
  49.         } else getAppDirectory(launchDir);
  50.     }
  51.  
  52.     if (languages = [NXApp systemLanguages]) {
  53.         while (!found && *languages) {
  54.             sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, nibFile);
  55.             if (!access(path, R_OK)) {
  56.                 retval = [NXApp loadNibFile:path owner:owner withNames:withNames fromZone:zone];
  57.                 found = YES;
  58.             }
  59.             if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
  60.             languages++;
  61.         }
  62.     }
  63.  
  64.     return found ? retval : [NXApp loadNibSection:nibFile owner:owner withNames:withNames fromZone:zone];
  65. }
  66.  
  67. id LocalImageRep(const char *file, NXZone *zone)
  68. {
  69.     id retval = nil;
  70.     char path[MAXPATHLEN+1];
  71.  
  72.     if (!launchDir || !*launchDir) {
  73.         if (retval = [NXApp delegate]) {
  74.             sprintf(launchDir,[retval launchDirectory]);
  75.             retval = NULL;
  76.         } else getAppDirectory(launchDir);
  77.     }
  78.  
  79.     if (languages = [NXApp systemLanguages]) {
  80.         while (!retval && *languages) {
  81.             sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
  82.             if (!access(path, R_OK)) retval = [[NXBitmapImageRep allocFromZone:zone] initFromFile:path];
  83.             if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
  84.             languages++;
  85.         }
  86.     }
  87.     return retval ? retval : [[NXBitmapImageRep allocFromZone:zone] initFromSection:file];
  88. }
  89.  
  90. id LocalImage(const char *file, NXZone *zone)
  91. {
  92.     id retval = nil;
  93.     char path[MAXPATHLEN+1];
  94.  
  95.     if (!launchDir || !*launchDir) {
  96.         if (retval = [NXApp delegate]) {
  97.             sprintf(launchDir,[retval launchDirectory]);
  98.             retval = NULL;
  99.         } else getAppDirectory(launchDir);
  100.     }
  101.  
  102.     if (languages = [NXApp systemLanguages]) {
  103.         while (!retval && *languages) {
  104.             sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
  105.             if (!access(path, R_OK)) retval = [[NXImage allocFromZone:zone] initFromFile:path];
  106.             if (!strcmp(*languages, NATIVE_LANGUAGE)) break;
  107.             languages++;
  108.         }
  109.     }
  110.     return retval ? retval : [[NXImage allocFromZone:zone] initFromSection:file];
  111. }
  112.  
  113. void LocalHelp(char *path, const char *file)
  114. {
  115.     id retval = NULL;
  116.  
  117.     if (!launchDir || !*launchDir) {
  118.         if (retval = [NXApp delegate]) sprintf(launchDir,[retval launchDirectory]);
  119.         else getAppDirectory(launchDir);
  120.     }
  121.  
  122.     if (languages = [NXApp systemLanguages]) {
  123.         while (*languages) {
  124.             sprintf(path, "%s/%s.lproj/%s", launchDir, *languages, file);
  125.             if (!access(path, R_OK)) return;
  126.             languages++;
  127.         }
  128.     }
  129.     sprintf(path, "%s/%s.lproj/%s", launchDir, NATIVE_LANGUAGE, file);
  130. }
  131.  
  132. char *LocalHelpFont()
  133. {
  134.     static char    EnglishFont[]  = "Helvetica";
  135.     static char    JapaneseFont[] = "GothicBBBHelvetica";
  136.  
  137.     languages = [NXApp systemLanguages];
  138.  
  139.     if (languages && *languages && !strcmp(*languages, "Japanese")) return JapaneseFont;
  140.     else return EnglishFont;
  141. }
  142.  
  143. char *LocalString(char *string)
  144. {
  145.     char    *rtnString;
  146.  
  147.     if (!stringSet) stringSet = [[NXApp delegate] stringSet:nil];
  148.  
  149.     if (!stringSet) return string;
  150.     rtnString = (char *)[stringSet valueForStringKey: string];
  151.     return rtnString ? rtnString : string;
  152. }
  153.  
  154. void getDateString(DAY_TIME *date, char *string)
  155. {
  156.     static int    order_type = -1;
  157.     static char *date_form = NULL;
  158.     static char *time_form = NULL;
  159.     static char *nodata = NULL;
  160.  
  161.     if (date->year < 0) {
  162.         if (nodata == NULL) nodata = LocalString("noDateData");
  163.         strcpy(string, nodata);
  164.     } else {
  165.         char buf[MESS_MAX];
  166.         if (order_type < 0) {
  167.             char *order = LocalString("date_order");
  168.             if (strncmp(order,"MDY",3) == 0) order_type = 1;
  169.             else if (strncmp(order,"YMD",3) == 0) order_type = 2;
  170.             else if (strncmp(order,"DMY",3) == 0) order_type = 3;
  171.         }
  172.         if (date_form == NULL) date_form = LocalString("date_form");
  173.  
  174.         switch(order_type) {
  175.         default:
  176.         case 1:
  177.             sprintf(buf, date_form, date->month, date->day, date->year);
  178.             break;
  179.         case 2:
  180.             sprintf(buf, date_form, date->year, date->month, date->day);
  181.             break;
  182.         case 3:
  183.             sprintf(buf, date_form, date->day, date->month, date->year);
  184.             break;
  185.         }
  186.  
  187.         if (date->hour > 0) {
  188.             char buf2[MESS_MAX];
  189.             if (time_form == NULL) time_form = LocalString("time_form");
  190.             sprintf(buf2, time_form, date->hour ,date->minute ,date->second);
  191.             strcpy(string, strcat(buf, buf2));
  192.         } else strcpy(string, buf);
  193.     }
  194. }
  195.